﻿@model OrderModel
@using SmartStore.Core.Domain.Tax;
@using SmartStore.Core.Domain.Catalog;

<style>
	.order-edit-cnt .edit-label {
		font-size: 0.75rem;
	}
	.order-edit-cnt .edit-value {
		max-width: 100%;
	}
</style>
   
<script>
		$(function() {

			@foreach (var item in Model.Items)
			{
				<text>toggleOrderItemEdit(</text>@(item.Id)<text>, false);</text>
			}

			// edit\delete order item
			$('#OrderItemTable').on('click', '.delete-order-item-button, .save-order-item-button', function() {
				var form = $('#AutoUpdateOrderItemForm'),
					table = $('#OrderItemTable'),
					orderItemId = $(this).attr('data-id');

				form.find('input[name=Id]').val(orderItemId);
				form.prop('action', $(this).attr('data-url'));

				if ($(this).hasClass('save-order-item-button')) {
					form.find('input[name=NewQuantity]').val(table.find('input[name=pvQuantity' + orderItemId + ']').val());
					form.find('input[name=NewUnitPriceInclTax]').val(table.find('input[name=pvUnitPriceInclTax' + orderItemId + ']').val());
					form.find('input[name=NewUnitPriceExclTax]').val(table.find('input[name=pvUnitPriceExclTax' + orderItemId + ']').val());
					form.find('input[name=NewTaxRate]').val(table.find('input[name=pvTaxRate' + orderItemId + ']').val());
					form.find('input[name=NewDiscountInclTax]').val(table.find('input[name=pvDiscountInclTax' + orderItemId + ']').val());
					form.find('input[name=NewDiscountExclTax]').val(table.find('input[name=pvDiscountExclTax' + orderItemId + ']').val());
					form.find('input[name=NewPriceInclTax]').val(table.find('input[name=pvPriceInclTax' + orderItemId + ']').val());
					form.find('input[name=NewPriceExclTax]').val(table.find('input[name=pvPriceExclTax' + orderItemId + ']').val());
				}

				$('#AutoUpdateOrderItemModal').modal();
			});
		});

		function toggleOrderItemEdit(itemId, editMode) {
			if (editMode) {
				$('#pnlEditUnitPrice' + itemId).show();
				$('#pnlEditQuantity' + itemId).show();
				$('#pnlEditDiscount' + itemId).show();
				$('#pnlEditPrice' + itemId).show();
				$('#btnEditOrderItem' + itemId).hide();
				$('#btnDeleteOrderItem' + itemId).hide();
				$('#btnAddReturnRequest' + itemId).hide();
				$('#btnSaveOrderItem' + itemId).removeClass("d-none");
				$('#btnCancelOrderItem' + itemId).removeClass("d-none");
			}
			else {
				$('#pnlEditUnitPrice' + itemId).hide();
				$('#pnlEditQuantity' + itemId).hide();
				$('#pnlEditDiscount' + itemId).hide();
				$('#pnlEditPrice' + itemId).hide();
				$('#btnEditOrderItem' + itemId).show();
				$('#btnDeleteOrderItem' + itemId).show();
				$('#btnAddReturnRequest' + itemId).show();
				$('#btnSaveOrderItem' + itemId).addClass("d-none");
				$('#btnCancelOrderItem' + itemId).addClass("d-none");
			}
		}
</script>

@if (Model.AutoUpdateOrderItemInfo.HasValue())
{
	<div class="alert alert-info alert-dismissible">
		<button type="button" class="close" data-dismiss="alert">&times;</button>
		@Html.Raw(Model.AutoUpdateOrderItemInfo)
	</div>
}

<div>
	<div class="t-toolbar t-grid-toolbar t-grid-top border border-bottom-0">
		<button type="submit" id="btnAddNewProduct" name="btnAddNewProduct" class="t-button t-button-primary" value="@T("Admin.Orders.Products.AddNew")"
				onclick="javascript:setLocation('@(Url.Action("AddProductToOrder", "Order", new { orderId = Model.Id }))'); return false;">
			<i class="fa fa-plus"></i>
			<span>@T("Admin.Orders.Products.AddNew")</span>
		</button>
	</div>
	<div class="table-responsive">
		<table id="OrderItemTable" class="table admin-table">
			<thead>
				<tr>
					<th>
						@T("Admin.Orders.Products.ProductName")
					</th>
					@if (Model.HasDownloadableProducts)
					{
						<th>
							@T("Admin.Orders.Products.Download")
						</th>
					}
					<th class="text-center">
						@T("Admin.Orders.Products.Price")
					</th>
					<th class="text-center">
						@T("Admin.Orders.Products.Quantity")
					</th>
					<th class="text-center">
						@T("Admin.Orders.Products.Discount")
					</th>
					<th class="text-center">
						@T("Admin.Orders.Products.Total")
					</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				@foreach (var item in Model.Items)
			{
					<tr>
						<td style="width: 25%;" class="align-top">
							<div>
								<div class="mb-2">
									@Html.LabeledProductName(item.ProductId, item.ProductName, item.ProductTypeName, item.ProductTypeLabelHint)
								</div>

								@if (!String.IsNullOrEmpty(item.AttributeInfo))
								{
									@Html.Raw(item.AttributeInfo)
								}
								@if (!String.IsNullOrEmpty(item.RecurringInfo))
								{
									<br />
									@Html.Raw(item.RecurringInfo)
								}
								@if (!String.IsNullOrEmpty(item.Sku))
								{
									<br />
									<span class="font-weight-medium">@T("Admin.Orders.Products.SKU")</span><text>:</text>
									@item.Sku
								}
								@if (item.ProductType == ProductType.BundledProduct)
								{
									<br />
									@BundleItemsInfo(item)
								}
								@if (item.ReturnRequests.Count > 0)
								{
									<br />
									<hr />
									for (int i = 0; i < item.ReturnRequests.Count; ++i)
									{
										var returnRequest = item.ReturnRequests[i];

										<span class="badge badge-@(returnRequest.StatusLabel) mr-1">@returnRequest.StatusString</span>

										<a href="@Url.Action("Edit", "ReturnRequest", new { id = returnRequest.Id })">
											@T("Admin.Orders.Products.ReturnRequest") @returnRequest.Id
										</a>

										<text>,</text>

										@T("Admin.ReturnRequests.Fields.Quantity")<text>&nbsp;</text>@returnRequest.Quantity
										if (i != item.ReturnRequests.Count - 1)
										{
											<br />
										}
									}
								}
								@if (item.PurchasedGiftCardIds.Count > 0)
								{
									<br />
									<hr />
									@T("Admin.Orders.Products.GiftCards")<text>:</text>

									for (int i = 0; i < item.PurchasedGiftCardIds.Count; i++)
									{
										<a href="@Url.Action("Edit", "GiftCard", new { id = item.PurchasedGiftCardIds[i] })">@item.PurchasedGiftCardIds[i]</a>
										if (i != item.PurchasedGiftCardIds.Count - 1)
										{
											<text>,</text>
										}
									}
								}
							</div>
						</td>

						@if (Model.HasDownloadableProducts)
						{
							<td align="center" style="width: 10%;" class="align-top">
								@if (item.IsDownload)
								{
									<div>
										@string.Format(T("Admin.Orders.Products.Download.DownloadCount").Text, item.DownloadCount)
										<input type="submit" name="btnResetDownloadCount@(item.Id)" value="@T("Admin.Orders.Products.Download.ResetDownloadCount")"
											   title="@T("Admin.Orders.Products.Download.ResetDownloadCount.Title")" id="btnResetDownloadCount@(item.Id)" class="btn btn-block btn-sm btn-secondary btn-to-danger mt-1" />
									</div>

									if (item.DownloadActivationType == DownloadActivationType.Manually)
									{
										<div>
											<input type="submit" name="btnPvActivateDownload@(item.Id)" @(item.IsDownloadActivated ? " value=" + T("Admin.Orders.Products.Download.Deactivate") : " value=" + T("Admin.Orders.Products.Download.Activate"))
												   id="btnPvActivateDownload@(item.Id)" class="btn btn-block btn-sm btn-success mt-2" />
										</div>
									}

									<div class="mt-1">
										<b class="font-weight-medium">@T("Admin.Orders.Products.License")</b>

										@if (item.LicenseDownloadId.HasValue && item.LicenseDownloadId.Value > 0)
										{
											<a href="@Url.Action("DownloadFile", "Download", new { downloadId = item.LicenseDownloadId.Value })">
												@T("Admin.Orders.Products.License.DownloadLicense")
											</a>
										}

										<input type="submit" id="pvlicensefile" name="pvlicensefile" value="@T("Admin.Orders.Products.License.UploadFile")"
											   onclick="javascript:openPopup('@(Url.Action("UploadLicenseFilePopup", "Order", new { id = Model.Id, orderItemId = item.Id, btnId = "btnRefreshPage", formId = "order-form" }))', true, false); return false;"
											   class="btn btn-sm btn-secondary btn-block mt-1" />
									</div>
								}
							</td>
						}

						<td style="width: 15%;" class="align-top">

							<div class="text-center">
								@if (Model.AllowCustomersToSelectTaxDisplayType)
								{
									@item.UnitPriceInclTax
									<br />
									@item.UnitPriceExclTax
								}
								else
								{
									@(Model.TaxDisplayType == TaxDisplayType.ExcludingTax ? item.UnitPriceExclTax : item.UnitPriceInclTax)
								}
							</div>

							<div id="pnlEditUnitPrice@(item.Id)" class="order-edit-cnt mt-3">
								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.InclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvUnitPriceInclTax@(item.Id)" type="text" value="@item.UnitPriceInclTaxValue" id="pvUnitPriceInclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.ExclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvUnitPriceExclTax@(item.Id)" type="text" value="@item.UnitPriceExclTaxValue" id="pvUnitPriceExclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.AddNew.TaxRate")</span>
										</div>
										<div class="col-6">
											<input name="pvTaxRate@(item.Id)" type="text" value="@item.TaxRate" id="pvTaxRate@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
							</div>
						</td>
						<td align="center" style="width: 5%" class="align-top">
							<div>
								@item.Quantity
							</div>

							<div id="pnlEditQuantity@(item.Id)" class="order-edit-cnt mt-3">
								<div class="mb-1">
									<input name="pvQuantity@(item.Id)" type="text" value="@item.Quantity" id="pvQuantity@(item.Id)" class="form-control form-control-sm edit-value" />
								</div>
							</div>
						</td>
						<td align="center" style="width: 14%;" class="align-top">

							<div>
								@if (Model.AllowCustomersToSelectTaxDisplayType)
								{
									@item.DiscountInclTax
									<br />
									@item.DiscountExclTax
								}
								else
								{
									@(Model.TaxDisplayType == TaxDisplayType.ExcludingTax ? item.DiscountExclTax : item.DiscountInclTax)
								}
							</div>

							<div id="pnlEditDiscount@(item.Id)" class="order-edit-cnt mt-3">
								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.InclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvDiscountInclTax@(item.Id)" type="text" value="@item.DiscountInclTaxValue" id="pvDiscountInclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.ExclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvDiscountExclTax@(item.Id)" type="text" value="@item.DiscountExclTaxValue" id="pvDiscountExclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
							</div>
						</td>
						<td align="center" style="width: 14%" class="align-top">

							<div>
								@if (Model.AllowCustomersToSelectTaxDisplayType)
								{
									@item.SubTotalInclTax
									<br />
									@item.SubTotalExclTax
								}
								else
								{
									@(Model.TaxDisplayType == TaxDisplayType.ExcludingTax ? item.SubTotalExclTax : item.SubTotalInclTax)
								}
							</div>

							<div id="pnlEditPrice@(item.Id)" class="order-edit-cnt mt-3">

								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.InclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvPriceInclTax@(item.Id)" type="text" value="@item.SubTotalInclTaxValue" id="pvPriceInclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>

								<div class="container mb-1">
									<div class="row">
										<div class="col-6">
											<span class="edit-label">@T("Admin.Orders.Products.Edit.ExclTax")</span>
										</div>
										<div class="col-6">
											<input name="pvPriceExclTax@(item.Id)" type="text" value="@item.SubTotalExclTaxValue" id="pvPriceExclTax@(item.Id)" class="form-control form-control-sm edit-value" />
										</div>
									</div>
								</div>
							</div>
						</td>

						<td class="omega text-truncate align-top" style="width: 27%;">

							@if (item.IsReturnRequestPossible)
							{
								<input type="submit" name="btnAddReturnRequest@(item.Id)" value="@T("Admin.Orders.Products.ReturnRequest.Create")" id="btnAddReturnRequest@(item.Id)"
									   class="btn btn-secondary" onclick='return confirm(@T("Admin.Common.AskToProceed").JsText);' />
							}

							<input type="submit" name="btnEditOrderItem@(item.Id)" value="@T("Admin.Common.Edit")" id="btnEditOrderItem@(item.Id)"
								   class="btn btn-warning" onclick="toggleOrderItemEdit(@(item.Id), true);return false;" />

							<input type="button" name="btnDeleteOrderItem@(item.Id)" value="@T("Admin.Common.Delete")" id="btnDeleteOrderItem@(item.Id)"
								   class="btn btn-danger delete-order-item-button" data-id="@(item.Id)" data-url="@Url.Action("DeleteOrderItem")" />

							<input type="button" name="btnSaveOrderItem@(item.Id)" value="@T("Admin.Common.Save")" id="btnSaveOrderItem@(item.Id)"
								   class="btn btn-warning save-order-item-button d-none" data-id="@(item.Id)" data-url="@Url.Action("EditOrderItem")" />

							<input type="submit" name="btnCancelOrderItem@(item.Id)" value="@T("Admin.Common.Cancel")" id="btnCancelOrderItem@(item.Id)"
								   class="btn btn-secondary d-none" onclick="toggleOrderItemEdit(@(item.Id), false);return false;" />
						</td>
					</tr>
			}
			</tbody>
		</table>
	</div>
</div>

@if (!String.IsNullOrEmpty(Model.CheckoutAttributeInfo))
{
	<div class="alert alert-info checkout-attributes">
		@Html.Raw(Model.CheckoutAttributeInfo)
	</div>
}
	
@helper BundleItemsInfo(OrderModel.OrderItemModel parentItem)
{
	if (parentItem.BundleItems != null)
	{
		<div class="bundle-items-container pt-2 pl-4">
			@foreach (var item in parentItem.BundleItems.OrderBy(x => x.DisplayOrder))
			{
				<div class="bundle-item mt-2">
					<div class="bundle-item-name">
						@Html.ActionLink(item.ProductName, "Edit", "Product", new { id = item.ProductId }, new { })
						@if (item.Quantity > 1)
						{
							<text>
								&#215;&nbsp;@item.Quantity
							</text>
						}
					</div>
					@if (!String.IsNullOrWhiteSpace(item.PriceWithDiscount))
					{
						<div class="bundle-item-price">
							@Html.Raw(item.PriceWithDiscount)
						</div>
					}
					@if (!String.IsNullOrEmpty(item.AttributeInfo))
					{
						<div class="bundle-item-attribute-info text-muted fs-sm">
							@Html.Raw(item.AttributeInfo)
						</div>
					}
				</div>
			}
		</div>
	}
}
